package cn.exrick.manager.controller;


import cn.exrick.common.pojo.KindEditorResult;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.QiniuUtil;
import cn.exrick.common.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


@RestController
@Api(description = "图片上传统一接口")
public class ImageController {

    @Value("${FILE_STORE_PATH}")
    private String storePath;
    @Value("${IMAGE_ACCESS_URL}")
    private String imageUrl;

    @RequestMapping(value = "/image/imageUpload",method = RequestMethod.POST)
    @ApiOperation(value = "WebUploader图片上传")
    public Result<Object> uploadFile(@RequestParam("file") MultipartFile files){
        String ext = FilenameUtils.getExtension(files.getOriginalFilename());
        String fileName = UUID.randomUUID().toString().replace("-", "") + "." + ext;
        try {
            File file=new File(storePath + File.separator + fileName);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }

            files.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ResultUtil<>().setData(fileName);
    }

    @RequestMapping(value = "/kindeditor/imageUpload",method = RequestMethod.POST)
    @ApiOperation(value = "KindEditor图片上传")
    public KindEditorResult kindeditor(@RequestParam("imgFile") MultipartFile files, HttpServletRequest request){

        KindEditorResult kindEditorResult=new KindEditorResult();
        //检查文件
        String message= QiniuUtil.isValidImage(request,files);
        if(!message.equals("valid")){
            kindEditorResult.setError(1);
            kindEditorResult.setMessage(message);
            return kindEditorResult;
        }

        String ext = FilenameUtils.getExtension(files.getOriginalFilename());
        String fileName = UUID.randomUUID().toString().replace("-", "") + "." + ext;
        try {
            File file=new File(storePath + File.separator + fileName);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }

            files.transferTo(file);
            kindEditorResult.setError(0);
            kindEditorResult.setUrl(imageUrl + fileName);
            return kindEditorResult;
        } catch (IOException e) {
            e.printStackTrace();
        }
        kindEditorResult.setError(1);
        kindEditorResult.setMessage("上传失败");
        return kindEditorResult;
    }


    @RequestMapping(value = "/image/base64Upload",method = RequestMethod.POST)
    public Result<Object> uploadBase64(String image){
        String fileName = UUID.randomUUID().toString().replace("-", "") + ".jpg";
        try {
            File file=new File(storePath + File.separator + fileName);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }

            byte[] imageBytes = Base64Utils.decode(image.getBytes());
            FileUtils.writeByteArrayToFile(file,imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultUtil<>().setErrorMsg("图片上传失败");
        }

        return new ResultUtil<>().setData(fileName);
    }
}
